# API URL 更新脚本
# 使用方法: ./scripts/update-api-url.ps1 "https://your-new-api-url.com"

param(
    [Parameter(Mandatory=$true)]
    [string]$NewUrl
)

# 验证URL格式
if (-not ($NewUrl -match '^https?://')) {
    Write-Error "URL必须以http://或https://开头"
    exit 1
}

# 移除尾随斜杠
$NewUrl = $NewUrl.TrimEnd('/')

$OldUrl = "https://bb9bdd4.r20.vip.cpolar.cn"

Write-Host "🔄 开始更新API URL..." -ForegroundColor Green
Write-Host "从: $OldUrl" -ForegroundColor Yellow
Write-Host "到: $NewUrl" -ForegroundColor Yellow

# 必须修改的配置文件
$ConfigFiles = @(
    "src/config/api-config.js",
    "config/global.js"
)

# 可选修改的调试日志文件
$LogFiles = @(
    "src/api/user.js"
)

# 修改配置文件
foreach ($file in $ConfigFiles) {
    if (Test-Path $file) {
        Write-Host "📝 更新配置文件: $file" -ForegroundColor Blue
        (Get-Content $file -Raw) -replace [regex]::Escape($OldUrl), $NewUrl | Set-Content $file -NoNewline
    } else {
        Write-Warning "⚠️  文件不存在: $file"
    }
}

# 修改调试日志文件
foreach ($file in $LogFiles) {
    if (Test-Path $file) {
        Write-Host "📝 更新调试日志: $file" -ForegroundColor Blue
        (Get-Content $file -Raw) -replace [regex]::Escape($OldUrl), $NewUrl | Set-Content $file -NoNewline
    } else {
        Write-Warning "⚠️  文件不存在: $file"
    }
}

Write-Host "✅ API URL更新完成!" -ForegroundColor Green
Write-Host "📋 请执行以下步骤验证:" -ForegroundColor Cyan
Write-Host "  1. npm run serve" -ForegroundColor White
Write-Host "  2. 测试登录功能" -ForegroundColor White
Write-Host "  3. 检查浏览器控制台的API调用" -ForegroundColor White 